package com.toogu.mazhangmen.ui.main;

import android.app.Activity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager2.widget.ViewPager2;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.toogu.mazhangmen.R;
import com.toogu.mazhangmen.bean.EquipmentsBean;
import com.toogu.mazhangmen.ui.view.RadioGroupView;
import com.toogu.mazhangmen.ui.view.ReSpinner;
import com.toogu.mazhangmen.ui.view.SearchView;
import com.toogu.mazhangmen.util.TokenUtil;
import com.toogu.mazhangmen.util.UiUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class MainView implements MainContract.IView{
    private static final String TAG = "MainView";
    private MainContract.IView.Delegate delegate = null;
    private Activity activity;
    private RadioGroupView radioGroupView;
    private ReSpinner spinner;
    private SearchView searchView;
    private TextView username;
    private ViewPager2 viewPager;
    private ProgressBar progressBar;
    private SmartRefreshLayout smartRefreshLayout;
    private List<ShowFragment> showFragmentList;
    private ViewPager2Adapter adapter;

    public MainView(Activity activity){
        this.activity=activity;
    }

    private void init(){
        progressBar = activity.findViewById(R.id.loading);
        progressBar.setVisibility(View.GONE);

        smartRefreshLayout = activity.findViewById(R.id.smartRefreshLayout);
        smartRefreshLayout.setOnRefreshListener(delegate.getOnRefreshLoadMoreListener());
    }
    private void initRadioGroup(){
        radioGroupView=activity.findViewById(R.id.RadioGroupView);
        radioGroupView.setInit(new String[]{"     全部     ","   紧急求助   ","门设备离线"});
        radioGroupView.setOnCheckedChangeListener(delegate.getRadioGroupViewOnCheckedChangeListener());
        radioGroupView.select(0);
    }
    private void initSpinner(){
        spinner=activity.findViewById(R.id.spinner);
        spinner.setOnItemSelectedListener(delegate.getAdapterViewOnItemSelectedListener());
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(activity, R.layout.view_spinner_item1, Collections.singletonList("请选择"));
        arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(arrayAdapter);

    }
    private void initSearch(){
        searchView=activity.findViewById(R.id.searchView);
        searchView.setClear();
        searchView.setOnSearchViewClickListener(delegate.getSearchViewOnClickListener());
    }
    private void initUser(){
        username=activity.findViewById(R.id.username);
        username.setText("值守员："+new TokenUtil().getName()+" ");
        username.setOnClickListener(delegate.getViewOnClickListener());
    }
    private void initViewPager(){
        viewPager=activity.findViewById(R.id.viewPager);
        showFragmentList =new ArrayList<>();
        adapter=new ViewPager2Adapter(((AppCompatActivity)activity).getSupportFragmentManager()
                ,((AppCompatActivity)activity).getLifecycle(), showFragmentList);
        viewPager.setOrientation(ViewPager2.ORIENTATION_VERTICAL);
        viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                if(showFragmentList.size()<position|| showFragmentList.size()==0) return;
                showFragmentList.get(position).reStart();
            }
        });
        viewPager.setAdapter(adapter);
        viewPager.setCurrentItem(0);
    }

    @Override
    public void onCreate() {
        init();//加载按钮
        initRadioGroup();//左上角的三个按钮
        initSpinner();//中间的选择
        initSearch();//搜索
        initUser();//用户
        initViewPager();//ViewPager
    }

    @Override
    public void onResume() {
        for(ShowFragment showFragment: showFragmentList){
            showFragment.reStart();
        }
    }

    @Override
    public void onPause() {
    }

    @Override
    public void onDestory() {
    }

    @Override
    public void setViewDelegate(Delegate viewDelegate) {
        delegate=viewDelegate;
    }

    @Override
    public void showNewViewPager(List<EquipmentsBean> equipmentsBeanList) {
        showFragmentList.clear();
        List<EquipmentsBean> temp = null;
        for(int i = 0; i< equipmentsBeanList.size(); i++){
            int j = i%UiUtil.getMonitorNumber();
            if(j==0) temp = new ArrayList<>();
            temp.add(equipmentsBeanList.get(i));
            if(j==UiUtil.getMonitorNumber()-1||i== equipmentsBeanList.size()-1){
                showFragmentList.add(new ShowFragment(temp));
            }
        }
        adapter.notifyDataSetChanged();
        viewPager.setCurrentItem(0);
    }

    @Override
    public void setSpinnerDate(String[] ProjectName) {
        List<String> name = new ArrayList<>();
        name.add(0,"请选择");
        name.addAll(Arrays.asList(ProjectName));
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(activity, R.layout.view_spinner_item1,name);
        arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(arrayAdapter);
    }

    @Override
    public void notify(EquipmentsBean equipmentsBean) {
        if (showFragmentList != null && equipmentsBean != null) {
            for (ShowFragment showFragment : showFragmentList) {
                if (showFragment.getEquipmentsBean2s().contains(equipmentsBean)) {
                    int position = showFragment.getEquipmentsBean2s().indexOf(equipmentsBean);
                    showFragment.notifyItem(position);
                    return;
                }
            }

            if(showFragmentList.size()==0){
                List<EquipmentsBean> list = new ArrayList<>();
                list.add(equipmentsBean);
                showFragmentList.add(new ShowFragment(list));
                adapter.notifyDataSetChanged();
            }else{
                ShowFragment showFragment = showFragmentList.get(showFragmentList.size() - 1);
                if (showFragment.getEquipmentsBean2s().size() < UiUtil.getMonitorNumber()) {
                    showFragment.addNewItem(equipmentsBean);
                } else {
                    List<EquipmentsBean> list = new ArrayList<>();
                    list.add(equipmentsBean);
                    showFragmentList.add(new ShowFragment(list));
                    adapter.notifyDataSetChanged();
                }
            }
        }
    }

    @Override
    public void showWaiting() {
        progressBar.setVisibility(View.VISIBLE);
        showFragmentList.clear();
        adapter.notifyDataSetChanged();
    }

    @Override
    public void hideWaiting() {
        progressBar.setVisibility(View.GONE);
        smartRefreshLayout.finishLoadMore();
        smartRefreshLayout.finishRefresh();
    }

    public void onSearch() {
        searchView.clearFocus();  //可以收起键盘
        radioGroupView.setWithoutChoice();//设置是空
        spinner.setSelection(0,false);//设置是第一位
    }

    public void onSpinner(int position) {
        searchView.clearFocus();  //可以收起键盘
        searchView.setClear();//搜索是空
        radioGroupView.setWithoutChoice();//设置是空
    }

    public void onRadioGroup() {
        searchView.clearFocus();  //可以收起键盘
        searchView.setClear();//搜索是空
        spinner.setSelection(0,false);//设置是第一位
    }

}
